package com.fivestar.pmanager.dao.mapper;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import com.fivestar.pmanager.core.mapper.DBActionMapper;
import com.fivestar.pmanager.core.mapper.PaginatingMapper;
import com.fivestar.pmanager.model.FsUserBean;


/**
 * 用户
 * FsUserMapper
 *
 * @author
 */
@Repository("fsUserMapper")
public interface FsUserMapper extends DBActionMapper<FsUserBean>,
        PaginatingMapper<FsUserBean> {

    @ResultMap("BaseResultMap")
    @Select("select * from fs_user where (name=#{loginId} or phone=#{loginId}) and is_deleted=0")
    FsUserBean loadByNameOrPhone(String loginId);

    @ResultMap("BaseResultMap")
    @Select("select * from fs_user where phone = #{phone} and is_deleted = '0'")
    FsUserBean getByPhone(String phone);

    @ResultMap("BaseResultMap")
    @Select("select * from fs_user where name = #{name} and is_deleted = '0'")
    FsUserBean getByName(String name);

    @ResultMap("BaseResultMap")
    @Select("select * from fs_user where phone = #{phone} and is_deleted = '0' and attribute = 'property'")
    FsUserBean getByPhoneForService(String phone);

    @ResultMap("BaseResultMap")
    @Select("select * from fs_user where cardno = #{cardno}")
    FsUserBean getValideByCardNo(String cardno);


    @ResultMap("BaseResultMap")
    @Select("select mu.*,pcd.name as deptName from fs_user mu\n" +
            " left join fs_user_dept mud on mud.user_id = mu.row_id\n" +
            " left join md_property_com_dept pcd on mud.dept_id = pcd.row_id " +
            " where 1=1 and mu.is_deleted=0 and mu.row_id=#{rowId} ")
    FsUserBean getSingleByParam(Integer arg0);

    /**
     * 根据属性查询用户
     *
     * @param attributes 用户属性列表
     * @return FsUserBean
     */
    @ResultMap("BaseResultMap")
    @Select("<script> select * from fs_user where is_deleted = '0' and attribute in" +
            "<foreach collection='array' open='(' close=')' separator=',' item='item'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    List<FsUserBean> findByAttributes(String[] attributes);

    @Results({
            @Result(column = "row_id", property = "id"),
            @Result(column = "nickname", property = "text")
    })
    @Select("select row_id,nickname from fs_user where name like '%${name}%' and attribute='property'")
    List<Map<String, Object>> findForSearch(FsUserBean FsUserBean);


    @Results({
            @Result(column = "row_id", property = "id"),
            @Result(column = "nickname", property = "text")
    })
    @Select("select row_id,nickname from fs_user where name like '%${name}%' ")
    List<Map<String, Object>> findForSearchAllUser(FsUserBean FsUserBean);

    List<Map<String,Object>> getListOfExcel(FsUserBean searchParam);

}
